package cn.com.blackboard.SISIntergrationBiz.util;

import java.io.IOException;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import blackboard.persist.PersistenceException;
import blackboard.platform.context.ContextManager;
import blackboard.platform.context.ContextManagerFactory;
import blackboard.platform.context.UnsetContextException;
import blackboard.platform.vxi.data.VirtualInstallation;
import blackboard.platform.vxi.service.VirtualSystemException;

public class SISDBCustSqlSessionFactory extends SqlSessionFactoryBean{
	
	private static final Logger logger = LoggerFactory.getLogger(SISDBCustSqlSessionFactory.class);
	
	SISDBCustSqlSessionFactory(){
		super();
		initSqlSessionFactory();		
	}
	
    public void initSqlSessionFactory(){
    	ContextManager cm = ContextManagerFactory.getInstance();
    	VirtualInstallation vi = null;
    	try {
			vi = cm.getContext().getVirtualInstallation();
		} catch (VirtualSystemException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (PersistenceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsetContextException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	logger.info("init sqlSessionFactory");
		String dbType = vi.getDbType(); //
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		String mapperUrl = "classpath:cn/com/blackboard/DataModule4B2/"+dbType+"/mapping/*.xml";
		try {
			super.setMapperLocations(resolver.getResources(mapperUrl));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	logger.info("init sqlSessionFactory finished");
	}
}
